<?php
final class Action {
	protected $file;
	protected $class;
	protected $method;
	protected $action;
	protected $args = array();
	
	public function __construct($action, $args = array()){
		$path = '';
		$this->action = $action;
		$parts = explode('/', str_replace('../', '', (string)$action));
		
		foreach ($parts as $part) {
			if (is_dir(DIR_APPLICATION .$path.$part.'/controller')) {
			
				$path .= $part.'/controller/';
				array_shift($parts);
			
				continue;
			}
			
			if (is_file(DIR_APPLICATION . str_replace(array('../', '..\\', '..'), '', $path) .ucfirst($part). 'Controller' . '.php')) {
				$this->file = DIR_APPLICATION . str_replace(array('../', '..\\', '..'), '', $path) .ucfirst($part). 'Controller' . '.php';
				$this->class =  preg_replace('/[^a-zA-Z0-9]/', '', ucfirst($part)).'Controller';
				
				array_shift($parts);
		
				break;
			}
		}
		
		if ($args) {
			$this->args = $args;
		}
			
		$method = array_shift($parts);
		
		if ($method) {
			$this->method = $method;
		} else {
			$this->method = 'index';
		}
	}
	
	public function __get($key) {
		$key = str_replace("_", "/", $key);
		return Singleton::getSingleton($key);
	}
	
	public function getFile() {
		return $this->file;
	}
	
	public function getClass() {
		return $this->class;
	}
	
	public function getMethod() {
		return $this->method;
	}
	
	public function getArgs() {
		return $this->args;
	}
	
	public function getAction(){
		return $this->action;
	}
}
?>